#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@desc: .

@Project ：kuberCloudTest
@File ：disks.py
@Author ：admin
@Date ：2024/3/21 14:20 
"""
from kuberCloudTest.cloud_resources.specifications.utils import specUtils, DiskIOps, DiskBandwidth
from kuberCloudTest.cloud_resources.urls import CloudResourcesUrls
from kuberCloudTest.common.constant import OFFSET, LIMIT, AsyncType
from kuberCloudTest.common.utils import _getDetail, _getOperationType
from kuberCloudTest.exception import KuberCloudException


def getDiskSpec(accessToken, specId=None, **kwargs):
    """
    获取云盘规格
    :param specId: 云盘规格UUID
    :param accessToken: access token
    :param kwargs: limit:
                   offset:
                   keyword: xxx
    :return:
    """
    spec_utils = specUtils(accessToken=accessToken)
    content = spec_utils.cloud_http_request.get(CloudResourcesUrls.getDiskSpec.value,
                                                {
                                                    'offset': kwargs.get('offset', OFFSET),
                                                    'limit': kwargs.get('limit', LIMIT),
                                                    'keyword': kwargs.get('keyword', ''),
                                                    'searchitem': kwargs.get('searchItem', 0)
                                                })
    if specId:
        total = content.get('total', 0)
        content = spec_utils.cloud_http_request.get(
            CloudResourcesUrls.getDiskSpec.value, {'limit': total, 'offset': OFFSET})
        content = _getDetail(content, specId, 'specid')
    return content


def createDiskSpec(accessToken, name, capacity: int, capacityUnit,
                   diskIOps: DiskIOps, diskBandwidth: DiskBandwidth, **kwargs):
    """
    创建云盘规格
    :param accessToken: access token
    :param name: 云盘规格名称
    :param capacity: 云盘规格容量 (1~1024 GB; 1-10 TB)
    :param capacityUnit: 容量单位 GB|TB
    :param diskIOps: 读写IOPS
    :param diskBandwidth: 带宽
    :param kwargs: remark：云盘规格描述
    :return:
    """

    def _convertCapacityUnit(unit):
        if unit not in ('GB', 'TB'):
            raise KuberCloudException('params error', 'capacityUnit error')
        return 1 if unit == 'GB' else 2

    def _convertReadAndWriteUnit(unit):
        return 1 if unit == 'GB/s' else 0

    operationType = 1101
    data = _getOperationType(operationType, AsyncType.SYNC.value)

    data['name'] = name
    data['capacity'] = capacity
    data['capacityUnit'] = _convertCapacityUnit(capacityUnit)

    data['diskiopstype'] = diskIOps.diskIOpsType
    data['disktotaliops'] = diskIOps.diskTotalIOps
    data['diskreadiops'] = diskIOps.diskReadIOps
    data['diskwriteiops'] = diskIOps.diskWriteIOps

    data['diskbandwidthtype'] = diskBandwidth.diskBandwidthType
    data['diskbandwidth'] = diskBandwidth.diskBandwidthNum
    data['diskbandwidthUnit'] = _convertReadAndWriteUnit(diskBandwidth.diskBandwidthUnit)
    data['readspeed'] = diskBandwidth.diskReadBandwidthNum
    data['readspeedUnit'] = _convertReadAndWriteUnit(diskBandwidth.diskReadBandwidthUnit)
    data['writespeed'] = diskBandwidth.diskWriteBandwidthNum
    data['writespeedUnit'] = _convertReadAndWriteUnit(diskBandwidth.diskWriteBandwidthUnit)

    data['remark'] = kwargs['remark'] if 'remark' in kwargs.keys() else ''
    return specUtils(accessToken=accessToken).cloud_http_request.post(
        CloudResourcesUrls.createDiskSpec.value, data=data)


def updateDiskSpec(accessToken, specId, **kwargs):
    """
    更新云盘规格
    :param accessToken: access token
    :param specId: 云盘规格uuid
    :param kwargs: name: 云盘规格名称
                   remark: 云盘规格描述
    :return:
    """
    diskSpec = getDiskSpec(accessToken=accessToken, specId=specId).get('obj', None)
    if not diskSpec:
        raise KuberCloudException('params error', 'diskSpec [{}] not exist'.format(specId))
    operationType = 1102
    data = _getOperationType(operationType, AsyncType.SYNC.value)

    data['specid'] = specId
    data['name'] = kwargs.get('name', diskSpec['name'])
    data['remark'] = kwargs.get('remark', diskSpec['remark'])
    return specUtils(accessToken=accessToken).cloud_http_request.post(
        CloudResourcesUrls.updateDiskSpec.value, data=data)


def startDiskSpec(accessToken, diskSpecIds):
    """
    启用云盘规格
    :param accessToken: access token
    :param diskSpecIds: 云盘规格uuid (多个使用英文逗号分割)
    :return:
    """
    operationType = 1104
    data = _getOperationType(operationType, AsyncType.SYNC.value)
    data['ids'] = diskSpecIds
    return specUtils(accessToken=accessToken).cloud_http_request.post(
        CloudResourcesUrls.startDiskSpec.value, data=data)


def stopDiskSpec(accessToken, diskSpecIds):
    """
    停用云盘规格
    :param accessToken: access token
    :param diskSpecIds: 云盘规格uuid (多个使用英文逗号分割)
    :return:
    """
    operationType = 1105
    data = _getOperationType(operationType, AsyncType.SYNC.value)
    data['ids'] = diskSpecIds
    return specUtils(accessToken=accessToken).cloud_http_request.post(
        CloudResourcesUrls.stopDiskSpec.value, data=data)


def deleteDiskSpec(accessToken, diskSpecIds):
    """
    删除云盘规格
    :param accessToken: access token
    :param diskSpecIds: 云盘规格uuid (多个使用英文逗号分割)
    :return:
    """
    operationType = 1103
    data = _getOperationType(operationType, AsyncType.SYNC.value)
    data['ids'] = diskSpecIds
    return specUtils(accessToken=accessToken).cloud_http_request.post(
        CloudResourcesUrls.deleteDiskSpec.value, data=data)


def globalShareDiskSpec(accessToken, diskSpecIds):
    """
    云盘规格全局共享
    :param accessToken: access token
    :param diskSpecIds: 云盘规格uuid (多个使用英文逗号分割)
    :return:
    """
    operationType = 1106
    data = _getOperationType(operationType, AsyncType.SYNC.value)
    data['ids'] = diskSpecIds
    return specUtils(accessToken=accessToken).cloud_http_request.post(
        CloudResourcesUrls.globalShareDiskSpec.value, data=data)


def globalRecallDiskSpec(accessToken, diskSpecIds):
    """
    云盘规格全局召回
    :param accessToken: access token
    :param diskSpecIds: 云盘规格uuid (多个使用英文逗号分割)
    :return:
    """
    operationType = 1107
    data = _getOperationType(operationType, AsyncType.SYNC.value)
    data['ids'] = diskSpecIds
    return specUtils(accessToken=accessToken).cloud_http_request.post(
        CloudResourcesUrls.globalRecallDiskSpec.value, data=data)
